package com.qiuhui.untils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;

/**
 * @ClassName: Mybatisuntils
 * @Description: TODO
 * @author: tian
 * @date: 2021/3/11  11:46 上午
 */
public class Mybatisuntils {
    static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            System.out.println();
        }
    }

    public static SqlSession getSqlSession() {
        /**
         * 既然有了 SqlSessionFactory，顾名思义，我们可以从中获得 SqlSession 的实例;
         * SqlSession 提供了在数据库执行 SQL 命令所需的所有方法;
         * 可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
         */
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

}
